package com.hanj.gulimall.product.controller;

import com.hanj.common.utils.R;
import com.hanj.gulimall.product.entity.CategoryEntity;
import com.hanj.gulimall.product.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 商品三级分类
 *
 * @author hanj
 * @email hanj.cn@outlook.com
 * @date 2022-01-14 21:39:58
 */
@RestController
@RequestMapping("/product/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @RequestMapping("/list/tree")
    public R list(@RequestParam Map<String, Object> params) {

        List<CategoryEntity> categories = categoryService.listWithTree();

        return R.ok().put("categories", categories);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {

        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody CategoryEntity category) {

        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody CategoryEntity category) {

        categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] catIds) {
        // 删除前要先检查当前目录是否被引用

        categoryService.removeMenuByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
